import {createRouter, createWebHistory} from 'vue-router'
import {getRouters} from '@/api/menu'

// 定义静态路由 - 这些路由总是会被加载
const constantRoutes = [
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/Login.vue'),
        meta: {title: '登录', hidden: true}
    },
    {
        path: '/',
        name: 'layout',
        component: () => import('@/layout/index.vue'),
        children: [
            {
                path: '',
                component: () => import('@/views/system/dashboard/index.vue'),
                meta: {title: '首页', icon: 'HomeFilled'}
            },
            {
                path: 'redis-monitor',
                name: 'RedisMonitor',
                component: () => import('@/views/system/dashboard/RedisMonitor'),
                meta: { title: 'Redis监控', icon: 'redis' }
            },
            // 将404页面放入布局容器内
            {
                path: '/404',
                name: 'NotFound',
                component: () => import('@/views/error/404.vue'),
                meta: {title: '404', hidden: true}
            },

            {
                path: '/500',
                name: 'Page500',
                component: () => import('@/views/error/500.vue'),
                meta: {
                title: '500错误'
                }
            },
            {
                path: '/user',
                name: 'user',
                component: () => import('@/views/system/user/index.vue'),
                meta: {title: '用户', hidden: true}
            },
            {
                path: '/user-sharding',
                name: 'userSharding',
                component: () => import('@/views/system/user/sharding.vue'),
                meta: {title: '用户分表管理', hidden: true}
            },
            {
                path: '/menu',
                name: 'menu',
                component: () => import('@/views/system/menu/index.vue'),
                meta: {title: '菜单', hidden: true}
            },
            {
                path: '/dict',
                name: 'dict',
                component: () => import('@/views/system/dict/index.vue'),
                meta: {title: '字典', hidden: true}
            },
            {
                path: '/role',
                name: 'role',
                component: () => import('@/views/system/role/index.vue'),
                meta: {title: '角色', hidden: true}
            },
            {
                path: '/role-auth/:roleId',
                name: 'roleAuth',
                component: () => import('@/views/system/role-auth/index.vue'),
                meta: {title: '角色授权', hidden: true},
                props: true
            },
            {
                path: '/swagger',
                name: 'swagger',
                component: () => import('@/views/system/swagger/index.vue'),
                meta: { title: 'API文档', icon: 'Document', hidden: true }
            },
            {
                path: '/druid',
                name: 'druid',
                component: () => import('@/views/system/druid/index.vue'),
                meta: { title: 'Druid监控', icon: 'Document', hidden: true }
            },
            {
                path: '/log',
                name: 'log',
                component: () => import('@/views/system/log/index.vue'),
                meta: { title: '系统日志', icon: 'Timer', hidden: true }
            },
            {
                path: '/product',
                name: 'product',
                component: () => import('@/views/system/product/index.vue'),
                meta: { title: '商品管理', icon: 'Timer', hidden: true }
            },
            {
                path: '/supplier',
                name: 'supplier',
                component: () => import('@/views/system/supplier/index.vue'),
            },
            {
                path: '/supplier/detail/:id',
                name: 'supplierDetail',
                component: () => import('@/views/system/supplier/detail.vue')
            },
        ]
    },

]

// 创建路由实例
const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes: constantRoutes
})

// 添加全局前置守卫
router.beforeEach((to, from, next) => {
    // 获取token
    const token = localStorage.getItem('token')
    // 如果访问的是登录页，直接放行
    if (to.path === '/login') {
        next()
        return
    }

    // 检查是否登录
    if (!token) {
        // 未登录，跳转到登录页
        next('/login')
        return
    }

    // 已登录，放行
    next()
})

// 添加动态路由的方法
// export function setupRouter() {
//     getRouters().then(res => {
//         if (res.code === 200) {
//             const data = res.data;
//             const accessedRoutes = handleAsyncRoutes(data);
//             accessedRoutes.forEach(route => {
//                 router.addRoute(route);
//
//                 // 移除现有的通配符路由
//                 router.removeRoute('catchAll');
//                 // 最后添加通配符路由
//                 router.addRoute({
//                     path: '/:pathMatch(.*)*',
//                     name: 'catchAll',
//                     redirect: '/404'
//                 });
//             });
//         }
//     })
// }
//
// // 处理异步路由
// function handleAsyncRoutes(routes) {
//     return routes.map(route => {
//         // 处理组件路径
//         if (route.component === 'Layout') {
//             route.component = () => import('@/layout/index.vue');
//         } else {
//             route.component = loadComponent(route.component);
//         }
//
//         // 递归处理子路由
//         if (route.children && route.children.length > 0) {
//             route.children = handleAsyncRoutes(route.children);
//         }
//
//         return route;
//     });
// }
//
// // 动态加载组件
// function loadComponent(component) {
//     return () => import(`@/views/${component}.vue`);
// }

export default router
